<?php

namespace App\Http\Controllers\Admin;

use App\Models\Admin\AdminMenu;
use App\Models\Admin\AdminUser;
use App\Models\Admin\Goods;
use App\Models\Admin\GoodsStore;
use App\Models\Admin\Store;
use App\Tools\AliSms;
use App\Tools\Financial;
use App\Tools\Images;
use App\Tools\Prstatic;
use App\Tools\Texcel;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Session;

class StoreController extends BaseController
{
    /**
     * 商户供应商列表
     */
    public function storeList()
    {
        $params = $this->requestArr;
        if ($params['export_excel'] == 1) {
            $list = Store::getPageList($params, 0);
            $title = '代理商管理-代理商列表';
            $titleArr = ['代理商编号', '代理商', '资质有效期', '状态', '申请时间', '基础折扣率', '货款余额', '管理费余额'];
            $dataArr = [];
            foreach ($list as $k => $item) {
                $dataArr[$k]['store_sn'] = $item['store_sn'];
                $dataArr[$k]['store_name'] = $item['store_name'];
                $dataArr[$k]['agent_end_time'] = $item['agent_end_time'];
                $dataArr[$k]['status'] = $item['status'];
                $dataArr[$k]['apply_time'] = $item['apply_time'];
                $dataArr[$k]['discount_rate'] = $item['discount_rate'];
                $dataArr[$k]['money_goods'] = $item['money_goods'];
                $dataArr[$k]['money_management'] = $item['money_management'];
            }
            if (empty($dataArr)) {
                view()->share('layout_message', '暂无导出数据');
            } else {
                Texcel::exportExcelHtml($title, $titleArr, $dataArr);
            }
            exit();
        }
        $list = Store::getPageList($params);

        if (!empty($params['name'])) {
            $list->appends(['name' => $params['name']])->render();
        }
        if (!empty($params['mobile'])) {
            $list->appends(['mobile' => $params['mobile']])->render();
        }
        if (!empty($params['status'])) {
            $list->appends(['status' => $params['status']])->render();
        }
        if (!empty($params['start_time'])) {
            $list->appends(['start_time' => $params['start_time']])->render();
        }
        if (!empty($params['end_time'])) {
            $list->appends(['end_time' => $params['end_time']])->render();
        }
        return view('admin/store/storeList', ['data' => $list]);
    }

    /**
     * 添加商户
     */
    public function storeAdd()
    {
        $params = $this->requestArr;
        if ($this->requestMethod == 'POST') {
            if ($params['discount_rate'] > 1 || $params['discount_rate'] < 0) {
                errorMsg('折扣比率不正确');
            }
            if (!$params['agent_start_time'] || !$params['agent_end_time']) {
                errorMsg('请填写资质期限');
            }
            $params['agent_start_time'] = strtotime($params['agent_start_time']);
            $params['agent_end_time'] = strtotime($params['agent_end_time']);
            if (($params['agent_end_time'] - $params['agent_start_time']) < 0) {
                errorMsg('资质时间填写错误');
            }
            $isStoreU = AdminUser::where(['mobile' => $params['user_mobile']])->value('id');
            if ($isStoreU) {
                errorMsg('登录账户手机号已被占用');
            }

            if ($params['id']) {
                Store::editInfoById($params['id'], $params);
                $storeId = $params['id'];
            } else {
                $isStore = Store::where(['principal_mobile' => $params['principal_mobile']])->value('id');
                if ($isStore) {
                    errorMsg('代理商手机号已被占用');
                }
                $storeId = Store::addInfo($params);
            }
            if ($storeId) {
                AdminUser::storeUserAdd($storeId, $params['user_name'], $params['user_password'], $params['user_mobile']);
                GoodsStore::adminAddInfo($storeId, $params['goods']);
                successMsg();
            }
            errorMsg();
        } else {
            if ($params['id']) {
                $info = Store::getInfoById($params['id']);
            } else {
                $info['store_sn'] = pdStoreSn();
                $info['company_license'] = '/static/admin/images/bg.png';
                $info['company_license_yy'] = '/static/admin/images/bg.png';
                $info['principal_card_a'] = '/static/admin/images/bg.png';
                $info['principal_card_b'] = '/static/admin/images/bg.png';
            }
            $statusList = Prstatic::$storeStatus;
            $goodsList = Goods::getBaseList();
            return view('admin/store/storeAddStep1', ['data' => $info, 'statusList' => $statusList, 'goodsList' => $goodsList]);
        }
    }

    /**
     * 商户修改
     */
    public function storeEdit()
    {
        $params = $this->requestArr;
        if ($this->requestMethod == 'POST') {
            if ($params['id']) {
                Store::editInfoById($params['id'], $params);
                successMsg();
            }
            errorMsg();
        } else {
            if ($params['id']) {
                $info = Store::getInfoById($params['id']);
            }
            $statusList = Prstatic::$storeStatus;
            return view('admin/store/storeEdit', ['data' => $info, 'statusList' => $statusList]);
        }
    }

    /**
     * 修改密码
     */
    public function storePassword()
    {
        $params = $this->requestArr;
        if ($this->requestMethod == 'POST') {
            if ($params['store_id']) {
                if (empty($params['password'])) {
                    errorMsg('请填写密码');
                }
                if (empty($params['password2'])) {
                    errorMsg('请填写确认密码');
                }
                if ($params['password'] != $params['password2']) {
                    errorMsg('两次密码输入不正确');
                }
                $safe = pbGetRandStr();
                $info['safe'] = $safe;
                $info['password'] = md5($params['password'] . $safe);
                AdminUser::where(['store_id' => $params['store_id'], 'username' => $params['store_sn']])->update($info);
                successMsg();
            }
            errorMsg();
        } else {
            if ($params['id']) {
                $info = Store::getInfoById($params['id']);
            }
            return view('admin/store/storePassword', ['data' => $info]);
        }
    }

    /**
     * 商品价格修改
     */
    public function storeGoodsEdit()
    {
        $params = $this->requestArr;
        if ($this->requestMethod == 'POST') {
            if ($params['id']) {
                GoodsStore::adminEditInfo($params['id'], $params['goods']);
                successMsg();
            }
            errorMsg();
        } else {
            if ($params['id']) {
                $info = Store::getInfoById($params['id']);
                if ($info) {
                    $info['is_add'] = 1;
                    $goodsList = Goods::getGoodsStoreList($params['id']);
                    return view('admin/store/storeGoodsEdit', ['data' => $info, 'goodsList' => $goodsList]);
                }
            }
            view()->share('layout_message', '暂无数据');
        }
    }

    /**
     * 添加新商品
     */
    public function storeGoodsAdd()
    {
        $params = $this->requestArr;
        if ($this->requestMethod == 'POST') {
            if ($params['id']) {
                GoodsStore::adminAddInfo($params['id'], $params['goods']);
                successMsg();
            }
            errorMsg();
        } else {
            if ($params['id']) {
                $info = Store::getInfoById($params['id']);
                if ($info) {
                    $info['is_add'] = 0;
                    $goodsList = Goods::getNewGoodsStoreList($params['id'], $info['discount_rate']);
                    return view('admin/store/storeGoodsAdd', ['data' => $info, 'goodsList' => $goodsList]);
                }
            }
            view()->share('layout_message', '暂无数据');
        }
    }

    /**
     * 商户充值
     * storeRecharge
     */
    public function storeRecharge()
    {
        $params = $this->requestArr;
        if ($this->requestMethod == 'POST') {
            if ($params['money_type'] && $params['store_id'] && $params['money']) {
                if ($params['money'] < 0) {
                    errorMsg('金额错误');
                }
                if ($params['money_type'] == 1) {
                    $to_account_id = Financial::getInfoByAid($params['store_id'], 1, 1, 'id');
                    Financial::moneyChanges(1, 1, $params['money'], $to_account_id);
                } elseif ($params['money_type'] == 2) {
                    $to_account_id = Financial::getInfoByAid($params['store_id'], 1, 2, 'id');
                    Financial::moneyChanges(2, 2, $params['money'], $to_account_id);
                }
                successMsg();
            }
            errorMsg();
        } else {
            $dataArr['money_type'] = $params['type'] ?: 1;
            if ($params['id']) {
                $info = Store::getInfoById($params['id']);
                if ($info) {
                    $dataArr['money'] = Financial::getInfoByAid($params['id'], 1, $dataArr['money_type'], 'money');
                    $dataArr['name'] = $dataArr['money_type'] == 1 ? '货款余额' : '管理费余额';
                    $dataArr['store_id'] = $params['id'];
                }
            }
            return view('admin/store/storeRecharge', $dataArr);
        }
    }

    /**
     * 代理商申请
     */
    public function storeApply()
    {
        $params = $this->requestArr;
        $list = Store::getApplyList($params);

        if (!empty($params['name'])) {
            $list->appends(['name' => $params['name']])->render();
        }
        if (!empty($params['mobile'])) {
            $list->appends(['mobile' => $params['mobile']])->render();
        }
        if (!empty($params['status'])) {
            $list->appends(['status' => $params['status']])->render();
        }
        if (!empty($params['start_time'])) {
            $list->appends(['start_time' => $params['start_time']])->render();
        }
        if (!empty($params['end_time'])) {
            $list->appends(['end_time' => $params['end_time']])->render();
        }
        return view('admin/store/storeApply', ['data' => $list]);
    }

    // 拒绝申请
    public function storeApplyUn()
    {
        $params = $this->requestArr;
        if ($this->requestMethod == 'POST') {
            if ($params['store_id']) {
                Store::where(['id' => $params['store_id']])->delete();
                successMsg();
            }
        }
        errorMsg();
    }
}